﻿using IFRS.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace IFRS.DataAccess.DAL
{
    public class SPR_DOUBTFUL
    {
        public DataTable SEARCH_ALL(Doubtful doubtful, Paging paging, ref int iRecordCount)
        {
            DataTable dt = new DataTable();
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@RecordCount", SqlDbType.Int));
                lstParams[0].Direction = ParameterDirection.Output;
                lstParams.Add(new SqlParameter("@PageIndex", paging.PageIndex));
                lstParams.Add(new SqlParameter("@PageSize", paging.PageSize));
                lstParams.Add(new SqlParameter("@Sorting", paging.Sorting));

                lstParams.Add(new SqlParameter("@TransactionDate", doubtful.TransactionDate.AsDBValue()));
                dt = Utility.StoredProcDataTable("SPR_DOUBTFUL_SEARCHALL", lstParams.ToArray());

                iRecordCount = (int)lstParams[0].Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dt;
        }

        public DataTable SEARCH_BYID(int DoubtfulID)
        {
            DataTable dt = new DataTable();
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@DoubtfulID", DoubtfulID));
                dt = Utility.StoredProcDataTable("SPR_DOUBTFUL_SEARCHBYID", lstParams.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dt;
        }

        public int INSERT(Doubtful doubtful)
        {
            int iNum = 0;
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();

            try
            {
                lstParams.Add(new SqlParameter("@DoubtfulID", SqlDbType.Int));
                lstParams[0].Direction = ParameterDirection.Output;
                lstParams.Add(new SqlParameter("@TransactionDate", doubtful.TransactionDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@Pass", doubtful.Pass.AsDBValue()));
                lstParams.Add(new SqlParameter("@SpecialMention", doubtful.SpecialMention.AsDBValue()));
                lstParams.Add(new SqlParameter("@Substandard", doubtful.Substandard.AsDBValue()));
                lstParams.Add(new SqlParameter("@DoubtfulValue", doubtful.DoubtfulValue.AsDBValue()));
                lstParams.Add(new SqlParameter("@DoubtfulLoss", doubtful.DoubtfulLoss.AsDBValue()));
                lstParams.Add(new SqlParameter("@IsConfirmed", doubtful.IsConfirmed.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedDate", doubtful.CreatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedBy", doubtful.CreatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedCostCenter", doubtful.CreatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedBranch", doubtful.CreatedBranch.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedDate", doubtful.UpdatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBy", doubtful.UpdatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", doubtful.UpdatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBranch", doubtful.UpdatedBranch.AsDBValue()));

                Utility.ExecuteStoredProc("SPR_DOUBTFUL_INSERT", lstParams.ToArray());

                iNum = (int)lstParams[0].Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }

        public int UPDATE(Doubtful doubtful)
        {
            int iNum = 0;
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@DoubtfulID", doubtful.DoubtfulID.AsDBValue()));
                lstParams.Add(new SqlParameter("@TransactionDate", doubtful.TransactionDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@Pass", doubtful.Pass.AsDBValue()));
                lstParams.Add(new SqlParameter("@SpecialMention", doubtful.SpecialMention.AsDBValue()));
                lstParams.Add(new SqlParameter("@Substandard", doubtful.Substandard.AsDBValue()));
                lstParams.Add(new SqlParameter("@DoubtfulValue", doubtful.DoubtfulValue.AsDBValue()));
                lstParams.Add(new SqlParameter("@DoubtfulLoss", doubtful.DoubtfulLoss.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedDate", doubtful.UpdatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBy", doubtful.UpdatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", doubtful.UpdatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBranch", doubtful.UpdatedBranch.AsDBValue()));

                iNum = (int)Utility.ExecuteStoredProc("SPR_DOUBTFUL_UPDATE", lstParams.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }
        public int DELETE(Doubtful doubtful)
        {
            int iNum = 0;
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@DoubtfulID", doubtful.DoubtfulID.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedDate", doubtful.UpdatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBy", doubtful.UpdatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", doubtful.UpdatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBranch", doubtful.UpdatedBranch.AsDBValue()));
                iNum = (int)Utility.ExecuteStoredProc("SPR_DOUBTFUL_DELETE", lstParams.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }
    }
}